package cn.cuiliangpeng.org.controller;

import cn.cuiliangpeng.basic.annotation.PreAuthorize;
import cn.cuiliangpeng.basic.utils.PageList;
import cn.cuiliangpeng.basic.utils.AjaxResult;
import cn.cuiliangpeng.org.domain.Department;
import cn.cuiliangpeng.org.query.DepartmentQuery;
import cn.cuiliangpeng.org.service.impl.DepartmentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口
 */
@RestController  //@Controller+@ResponseBody
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentServiceImpl departmentService;

    /**
     * 查询单个部门
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过ID查询部门")
    @GetMapping("/{id}")   //Get  http://localhost/department/1
    public Department findById(@PathVariable("id") Long id) {  //@PathVariable("id") Long id 从路劲上去获取变量id 赋值给Long id
        return departmentService.findById(id);
    }

    /**
     * 查询所有部门
     *
     * @return
     */
    @ApiOperation(value = "查询所有部门")
    @GetMapping  //Get  http://localhost/department
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 修改与添加
     *
     * @param department
     */
    @ApiOperation(value = "添加与修改部门")
    @PutMapping  //put http://localhost/department 对象
    public AjaxResult updateAndAdd(@RequestBody Department department) { //@RequestBody取对象使用
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
    }

    /**
     * 删除单个部门
     *
     * @param id
     */
    @ApiOperation(value = "通过id删除部门")
    @DeleteMapping("/{id}")  //delete  http://localhost/department/1
    @PreAuthorize(name = "通过id删除部门",value = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {  //@PathVariable("id") Long id 从路劲上去获取变量id 赋值给Long id
        departmentService.delete(id);
        return AjaxResult.me();
    }
    /**
     * 分页
     * @param departmentQuery
     * @return
     */
    @PostMapping  //post http://localhost/department 对象
    @ApiOperation("分页查询")
    private PageList<Department> querypage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    @PatchMapping
    @ApiOperation("批量删除对象")
    public AjaxResult patch(@RequestBody Long[] ids){
            departmentService.patch(ids);
            return AjaxResult.me();
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/tree")
    public List<Department> departmentTree(){
        return departmentService.departmentTree();
    }
}
